package com.iaiai.cobra.web.controller;

import com.iaiai.cobra.common.constant.YesOrNo;
import com.iaiai.cobra.common.util.FileUtil;
import com.iaiai.cobra.common.vo.ResultVo;
import com.iaiai.cobra.core.BaseController;
import com.iaiai.cobra.repository.bean.Menu;
import com.iaiai.cobra.repository.bean.Role;
import com.iaiai.cobra.repository.bean.UploadFile;
import com.iaiai.cobra.repository.bean.User;
import com.iaiai.cobra.repository.bean.po.MenuPo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * Created with IndexController.
 * Package: com.iaiai.cobra.web.controller
 * Author: iaiai
 * Create Time: 2024/6/28 21:53
 * QQ: 176291935
 * Email: 176291935@qq.com
 * Description:
 */
@RestController
public class IndexController extends BaseController {

    //上传图片
    @PostMapping(value = "/upload/img.do")
    public ResultVo uploadImg(@RequestParam("file") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();   //文件名
        String suffix = FileUtil.getFileSuffix(originalFilename);   //文件后缀
        long size = file.getSize();   //文件大小

        //把文件上传到oss或cos上，代码自己对接，这里写一个返回固定的图片
        String webUrl = "https://pic.rmb.bdstatic.com/bjh/down/5d1334f614e50fd2d79966315a36de10.png";

        UploadFile uploadFile = new UploadFile();
        uploadFile.setDel(YesOrNo.NO.getValue());
        uploadFile.setCreateTime(new Date());
        uploadFile.setModifyTime(uploadFile.getCreateTime());
        uploadFile.setWebUrl(webUrl);
        uploadFile.setSize(size);
        uploadFile.setSuffix(suffix);
        uploadFile.setFilename(originalFilename);
        uploadFile.setOperatorUserId(getOnline().getUser().getId());
        serviceManager.uploadFileService.save(uploadFile);

        return success(uploadFile);
    }

    //查询菜单项
    @PostMapping("/nav/menu.json")
    public ResultVo navMenus() throws IOException {
        Map<String,Object> map = new HashMap<>();

        User user = getOnline().getUser();
        List<Role> roles = serviceManager.roleService.queryByUser(user.getId());
        if(roles==null || roles.size()<=0)roles = new ArrayList<>();
        String roleIds = "";
        for (Role role : roles) {
            if(roleIds.length()>0)roleIds+=",";
            roleIds += role.getId().toString();
        }

        //菜单，包含目录跟菜单
        List<MenuPo> menus = serviceManager.menuService.queryRoleMenu(roleIds);
        map.put("menus",menus);

        return success(map);
    }

    //查询路由页面
    @PostMapping("/route.json")
    public ResultVo route() throws IOException {
        Map<String,Object> map = new HashMap<>();

        User user = getOnline().getUser();

        //包含菜单跟子页面
        List<Menu> menus = serviceManager.menuService.queryPageMenu(user.getId());
        map.put("menus",menus);

        return success(map);
    }

}
